package com.black.web.controller.system;

import com.black.common.annotation.Log;
import com.black.common.core.controller.BaseController;
import com.black.common.core.domain.R;
import com.black.common.enums.BusinessType;
import com.black.system.domain.req.system.add.SysNoticeAddReq;
import com.black.system.domain.req.system.edit.SysNoticeEditReq;
import com.black.system.domain.req.system.query.SysNoticeQueryReq;
import com.black.system.domain.resp.system.SysNoticeResp;
import com.black.system.service.SysNoticeService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author L
 */
@Api(tags = "后台-通知公告Api")
@RestController
@RequestMapping("/admin/notice")
public class SysNoticeController extends BaseController {

    @Autowired
    private SysNoticeService sysNoticeService;

    @PreAuthorize("@ss.hasPermi('system:notice:list')")
    @ApiOperation(value = "分页查询")
    @GetMapping("/list")
    public R<PageInfo<SysNoticeResp>> list(SysNoticeQueryReq req) {
        PageInfo<SysNoticeResp> page = this.sysNoticeService.paginationQuery(req);
        return R.ok(page);
    }

    @PreAuthorize("@ss.hasPermi('system:notice:query')")
    @ApiOperation(value = "根据id查询")
    @GetMapping("/{id}")
    public R<SysNoticeResp> findById(@PathVariable(value = "id") Long id) {
        SysNoticeResp sysNoticeResp = this.sysNoticeService.findById(id);
        return R.ok(sysNoticeResp);
    }

    @Log(title = "通知公告", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('system:notice:add')")
    @ApiOperation(value = "添加")
    @PostMapping
    public R add(@RequestBody @Valid SysNoticeAddReq req) {
        boolean res = this.sysNoticeService.add(req);
        return super.isSuccess(res);
    }

    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('system:notice:edit')")
    @ApiOperation(value = "修改")
    @PutMapping
    public R edit(@RequestBody @Valid SysNoticeEditReq req) {
        boolean res = this.sysNoticeService.edit(req);
        return super.isSuccess(res);
    }

    @Log(title = "通知公告", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('system:notice:remove')")
    @ApiOperation(value = "删除")
    @DeleteMapping(value = "/{ids}")
    public R delete(@PathVariable(value = "ids") Long[] ids) {
        boolean res = this.sysNoticeService.deleteById(ids);
        return super.isSuccess(res);
    }
}
